<template>
  <div class="recommend">
    <div class="title">
      <h2>精品推荐</h2>
    </div>
    <div class="items">
      <div class="item"
        @click="clickItem(item.id)"
        v-for="item in items" :key="item.id">
        <img v-lazy="item.pic" alt="" class="item-left">
        <div class="item-info">
          <h3 class="name">{{item.name}}</h3>
          <p class="desc">
            {{ item.characteristic ? item.characteristic: '暂无描述' | filterDesc}}
          </p>
          <div class="item-action">
            <p class="price">
              {{ item.minPrice | currency }}
            </p>
            <van-icon
              @click.stop="addCart(item.id)"
              class-prefix="icon" color="#f88" size="22" name="jia"></van-icon>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    items: {
      type: Array,
      required: true
    }
  },
  methods: {
    addCart (id) {
      // 点击加入购物车按钮
      this.$emit('addCart', id)
    },
    clickItem (id) {
      this.$emit('clickItem', id)
    }
  },
  filters: {
    filterDesc (v, len = 10) {
      if (v.length > 10) {
        return v.substr(0, len) + '...'
      } else {
        return v
      }
    },
    currency (v, sign = '￥') {
      return sign + v
    }
  }
}
</script>

<style lang="scss" scoped>
.title{
  padding: 0 10px;
  line-height: 40px;
  color: #333;
  background-color: #f1f1f1;
  h2{
    font-weight: 100;
    font-size: 16px;
  }
}

.items{
  .item{
    display: flex;
    &-left{
      width: 160px;
      height: 160px;
    }
    &-info{
      flex:1;
      padding:0 10px;
      box-sizing: border-box;
      color: #333;
      .name{
        font-size: 16px;
         color: #333;
         line-height: 30px;
      }
      .desc{
         font-size: 14px;
         color: #666;
      }
      .item-action{
        display: flex;
        margin-top: 10px;
        justify-content: space-between;
        align-items: center;
        p.price{
          color:#f88;
        }
      }
    }
  }
}
</style>
